﻿using Epoxy;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Imaging;

namespace EpoxyDemo
{
    [ViewModel]
    internal class MainWindowViewModel
    {
        #region 属性
        public string WindowTitle { get; private set; }
        public ObservableCollection<Student> Students { get; private set; }
        public Student? SelectedStudent { get; set; }
        public Pile<Image> Img { get; } = Pile.Factory.Create<Image>();
        public WindowState WindowState { get; set; }
        public Well<Button> FountainWellDemoWell { get; } = Well.Factory.Create<Button>();
        #endregion
        #region 命令
        public Command? WindowLoadedCommand { get;}
        public Command? ClosingCommand { get;}
        public Command? ShowStudentInfoCommand { get;}
        public Command? AddStudentCommand { get; }
        public Command? DeleteStudentCommand { get; }
        #endregion

        #region 方法
        public MainWindowViewModel()
        {
            this.WindowLoadedCommand = Command.Factory.Create(WindowLoaded);
            this.ClosingCommand = Command.Factory.Create<CancelEventArgs>(async args => 
            {
                args.Cancel = true;
                WindowState = WindowState.Minimized;

            });
            this.ShowStudentInfoCommand = Command.Factory.Create(ShowStudentInfo);
            this.AddStudentCommand = Command.Factory.Create(async () => 
            {
                await Task.Delay(500);
                this.Students?.Add(new Student { Id = this.Students.Count+1, Name = "New Student" });
            });

            this.DeleteStudentCommand = Command.Factory.Create(DeleteStudent);

            this.FountainWellDemoWell.Add<RoutedEventArgs>("Click",async args => 
            {
                await Task.Run(async () =>
                {
                    await UIThread.InvokeAsync(async () => 
                    {
                        var btn = args.Source as Button;
                        btn.Content = "Button Clicked!!!";
                        await Task.Delay(2000);
                        btn.Content = "FountainWell";
                    });
                });
            });
        }

        private async ValueTask WindowLoaded()
        {
            this.WindowTitle = "学生管理系统";

            this.Students = new ObservableCollection<Student>();
            this.Students.Add(new Student { Id = 1, Name = "Mike" });

            await this.Img.RentAsync(async img =>
            {
                img.Source = new BitmapImage(new Uri("https://youimg1.c-ctrip.com/target/100a0g0000007n7d636CA.jpg"));
            });
        }

        private async ValueTask ShowStudentInfo()
        {
            if (this.SelectedStudent == null) { return; }
            MessageBox.Show(this.SelectedStudent.Name);
        }

        private async ValueTask DeleteStudent()
        {
            if (this.SelectedStudent == null) { return; }
            this.Students.Remove(this.SelectedStudent);
        }

        [PropertyChanged(nameof(SelectedStudent))]
        private async ValueTask OnSelectedStudentChanged(Student student)
        {
            
        }
        #endregion
    }
}
